#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "ite\itu.h"
#include "scene.h"
#include "ctrlboard.h"
#include "mg_Lcd_Driver.h"
#include "mg_Device.h"
#include "mg_Rs485Protocol.h"

/* widgets:
Layer_Config
Background_Config
Wheel2
StepWheel1
Animation28
Container_SetVoiceDis
Text37
PopupButton39
Container_SetBacklightDis
Text30
PopupButton35
Container_SetBacklightEn
Text_SetBacklight
PopupButton_SetBacklight
CoverFlow_Config
Container_SetMainmenu
PopupButton_SetMainmenu
Container_SetScreen
PopupButton_SetScreen
Button_Back_MainMenu
*/

#define CONFIG_NUM			9

//更新图标和文字的大小及位子
void Update_IconText_Position(void);

static char WidgetName[30];
static ITUBackground* Background_Config;
static ITUWheel *Wheel_Config;
static ITUText* stepWheelChild[CONFIG_NUM] = { 0 };
static ITUText* Text_Config[CONFIG_NUM] = { 0 };
static ITUIcon* Icon_Config[CONFIG_NUM] = { 0 };
static ITUIcon* Icon_Config2[CONFIG_NUM] = { 0 };
static ITUPopupButton *PopupButton_Config_Select;
static ITUContainer*Container_ConfigText, *Container_ConfigIcon;
static int focusSize, h_SW, w_SW, resize_w, resize_h, repos_X, repos_Y;
bool ConfigOnEnter(ITUWidget* widget, char* param)
{
	uint8_t i;
	if (!Background_Config)
	{
		Background_Config = ituSceneFindWidget(&theScene, "Background_Config");
		assert(Background_Config);
		Wheel_Config = ituSceneFindWidget(&theScene, "Wheel_Config");
		assert(Wheel_Config);
		Container_ConfigText = ituSceneFindWidget(&theScene, "Container_ConfigText");
		assert(Container_ConfigText);
		Container_ConfigIcon = ituSceneFindWidget(&theScene, "Container_ConfigIcon");
		assert(Container_ConfigIcon);
		for (i = 0; i < CONFIG_NUM; i++)
		{
			//获取Wheel的子部件
			stepWheelChild[i] = (ITUText*)itcTreeGetChildAt(Wheel_Config, i);
			assert(stepWheelChild[i]);
			//获取自定义的Text和Icon
			sprintf(WidgetName, "Icon_Config%d", i);
			Icon_Config[i] = ituSceneFindWidget(&theScene, WidgetName);
			assert(Icon_Config[i]);
			sprintf(WidgetName, "Icon_Config2_%d", i);
			Icon_Config2[i] = ituSceneFindWidget(&theScene, WidgetName);
			assert(Icon_Config2[i]);
			sprintf(WidgetName, "Text_Config%d", i);
			Text_Config[i] = ituSceneFindWidget(&theScene, WidgetName);
			assert(Text_Config[i]);
		}
	}
	for (i = 0; i < CONFIG_NUM; i++)
	{
		if (Sys.Language == MG_LANGUAGE_CHS)//中文
			Text_Config[i]->fontIndex = 5;
		else if (Sys.Language == MG_LANGUAGE_ENG)
			Text_Config[i]->fontIndex = 7;
		else if (Sys.Language == MG_LANGUAGE_ZHTW)
			Text_Config[i]->fontIndex = 8;
	}
	h_SW = ituWidgetGetHeight(stepWheelChild[0]);//焦点高
	w_SW = ituWidgetGetWidth(stepWheelChild[0]);//焦点宽
	focusSize = stepWheelChild[Wheel_Config->focusIndex]->fontHeight;//焦点的字体高
	Update_IconText_Position();
	
	Set_Rotate_Block(120);//旋转步进
    return true;
}

bool ConfigOnLeave(ITUWidget* widget, char* param)
{
    return true;
}

bool ConfigOnTimer(ITUWidget* widget, char* param)
{
	static uint8_t Interval = 15;
	//用户手势操作
	if (Action_Cmd.User_Action == Left_Slide || Action_Cmd.User_Action == Right_Slide)
	{
		GotoMainMenu();//返回主菜单
		Clear_User_Action();
	}
	//用户操作
	if (Action_Cmd.User_Action == Anticlockwise_Rotation)
	{
		if (Interval >= 15 && ituWheelCheckIdle(Wheel_Config))//间隔时间，不能频繁操作
		{
			Interval = 0;
			ituWheelPrev(Wheel_Config);
		}
	}
	else if (Action_Cmd.User_Action == Clockwise_Rotation)
	{
		if (Interval >= 15 && ituWheelCheckIdle(Wheel_Config))//间隔时间，不能频繁操作
		{
			Interval = 0;
			ituWheelNext(Wheel_Config);
		}
	}
	Clear_User_Action();
	if (Interval < 15)Interval++;
	Update_IconText_Position();
    return true;
}
bool Button_Config_Select_On_Press(ITUWidget* widget, char* param)
{
	ITULayer* JumpLayer;
	switch (Wheel_Config->focusIndex)
	{
	case 0:
		JumpLayer = ituSceneFindWidget(&theScene, "Layer_Config_SetLight");//亮度设置
		assert(JumpLayer);
		ituLayerGoto(JumpLayer);
		break;
	case 1:
		JumpLayer = ituSceneFindWidget(&theScene, "Layer_Config_SetScreensaver");//屏保设置
		assert(JumpLayer);
		ituLayerGoto(JumpLayer);
		break;
	case 2:
		JumpLayer = ituSceneFindWidget(&theScene, "Layer_Config_DemoMode");//演示模式
		assert(JumpLayer);
		ituLayerGoto(JumpLayer);
		break;
	case 3:
		JumpLayer = ituSceneFindWidget(&theScene, "Layer_Config_Language");//语言设置
		assert(JumpLayer);
		ituLayerGoto(JumpLayer);
		break;
	case 4:
		JumpLayer = ituSceneFindWidget(&theScene, "Layer_Config_SetMainmenu");//菜单选择
		assert(JumpLayer);
		ituLayerGoto(JumpLayer);
		break;
	case 5:
		JumpLayer = ituSceneFindWidget(&theScene, "Layer_Config_SetVoice");//声音设置
		assert(JumpLayer);
		ituLayerGoto(JumpLayer);
		break;
	case 6:
		JumpLayer = ituSceneFindWidget(&theScene, "Layer_Config_SetLightRing");//灯光设置
		assert(JumpLayer);
		ituLayerGoto(JumpLayer);
		break;
	case 7:
		GotoMainMenu();//返回菜单
		break;
	case 8:
		JumpLayer = ituSceneFindWidget(&theScene, "Layer_Config_AboutDevice");//关于设备
		assert(JumpLayer);
		ituLayerGoto(JumpLayer);
		break;
	default://防止意外情况
		JumpLayer = ituSceneFindWidget(&theScene, "Layer_Config_SetLight");
		assert(JumpLayer);
		ituLayerGoto(JumpLayer);
		break;
	}
}
bool SlideRightToBackConfig(ITUWidget* widget, char* param)
{
	printf("Moorgen INFO:Goto Config by touch.\n");
	GotoConfigLayer();
	return true;
}
void GotoConfigLayer(void)
{
	ITULayer* ConfigLayer;
	ConfigLayer = ituSceneFindWidget(&theScene, "Layer_Config");
	ituLayerGoto(ConfigLayer);
}
//更新图标和文字的大小及位子
void Update_IconText_Position(void)
{
	int i;
	uint8_t IconSize_Width, IconSize_Height;
	uint8_t TextSize_Width, TextSize_Height;
	float Position_Percentage = 0;
	for (i = 0; i < CONFIG_NUM; i++)
	{
		resize_w = w_SW * stepWheelChild[i]->fontHeight / focusSize;
		resize_h = h_SW * stepWheelChild[i]->fontHeight / focusSize;
		repos_X = (ituWidgetGetX(stepWheelChild[i]) + w_SW - resize_w) / 2;//Wheel子类坐标
		repos_Y = ituWidgetGetY(stepWheelChild[i]);//Wheel子类坐标
		if (repos_Y >= 0 && repos_Y <= 78)
		{
			Position_Percentage = (78 - (float)repos_Y) / 78;
			ituWidgetSetWidth(Icon_Config[i], 73 - 20 * Position_Percentage);
			ituWidgetSetHeight(Icon_Config[i], 73 - 20 * Position_Percentage);
			ituWidgetSetAlphaImpl(Icon_Config[i], 255 - 255 * Position_Percentage);
			ituWidgetSetWidth(Icon_Config2[i], 73 - 20 * Position_Percentage);
			ituWidgetSetHeight(Icon_Config2[i], 73 - 20 * Position_Percentage);
			ituWidgetSetAlphaImpl(Icon_Config2[i], 255 * Position_Percentage);
			if (Sys.Language == MG_LANGUAGE_CHS)
				ituTextSetFontSize(Text_Config[i], 30 - 10 * Position_Percentage);
			else if (Sys.Language == MG_LANGUAGE_ENG || Sys.Language == MG_LANGUAGE_ZHTW)
				ituTextSetFontSize(Text_Config[i], 24 - 8 * Position_Percentage);
			ituWidgetSetAlphaImpl(Text_Config[i], 255 - 128 * Position_Percentage);
		}
		else if (repos_Y>78 && repos_Y <= 156)
		{
			Position_Percentage = (156 - (float)repos_Y) / 78;
			ituWidgetSetWidth(Icon_Config[i], 53 + 20 * Position_Percentage);
			ituWidgetSetHeight(Icon_Config[i], 53 + 20 * Position_Percentage);
			ituWidgetSetAlphaImpl(Icon_Config[i], 255 * Position_Percentage);
			ituWidgetSetWidth(Icon_Config2[i], 53 + 20 * Position_Percentage);
			ituWidgetSetHeight(Icon_Config2[i], 53 + 20 * Position_Percentage);
			ituWidgetSetAlphaImpl(Icon_Config2[i], 255 - 255 * Position_Percentage);
			if (Sys.Language == MG_LANGUAGE_CHS)
				ituTextSetFontSize(Text_Config[i], 20 + 10 * Position_Percentage);
			else if (Sys.Language == MG_LANGUAGE_ENG || Sys.Language == MG_LANGUAGE_ZHTW)
				ituTextSetFontSize(Text_Config[i], 16 + 8 * Position_Percentage);
			ituWidgetSetAlphaImpl(Text_Config[i], 128 + 127 * Position_Percentage);
		}
		else
		{
			ituWidgetSetWidth(Icon_Config[i], 53);
			ituWidgetSetHeight(Icon_Config[i], 53);
			ituWidgetSetAlphaImpl(Icon_Config[i], 0);
			ituWidgetSetWidth(Icon_Config2[i], 53);
			ituWidgetSetHeight(Icon_Config2[i], 53);
			ituWidgetSetAlphaImpl(Icon_Config2[i], 255);
			if (Sys.Language == MG_LANGUAGE_CHS)
				ituTextSetFontSize(Text_Config[i], 20);
			else if (Sys.Language == MG_LANGUAGE_ENG || Sys.Language == MG_LANGUAGE_ZHTW)
				ituTextSetFontSize(Text_Config[i], 16);
			ituWidgetSetAlphaImpl(Text_Config[i], 128);
		}

		ituWidgetSetPosition(Text_Config[i], repos_X + 80, repos_Y + 30 - 6 * (Text_Config[i]->fontWidth - 20) / 10);//文本位置
		ituWidgetSetVisible(Text_Config[i], true);

		IconSize_Width = ituWidgetGetWidth(Icon_Config[i]);
		IconSize_Height = ituWidgetGetHeight(Icon_Config[i]);
		ituWidgetSetPosition(Icon_Config[i], repos_X + (73 - IconSize_Width) / 2 + 14 + 34 * (73 - IconSize_Width) / 20, repos_Y + (73 - IconSize_Height) / 2 + 6);//图片位置
		ituWidgetSetVisible(Icon_Config[i], true);
		IconSize_Width = ituWidgetGetWidth(Icon_Config2[i]);
		IconSize_Height = ituWidgetGetHeight(Icon_Config2[i]);
		ituWidgetSetPosition(Icon_Config2[i], repos_X + (73 - IconSize_Width) / 2 + 14 + 34 * (73 - IconSize_Width) / 20, repos_Y + (73 - IconSize_Height) / 2 + 6);//图片位置
		ituWidgetSetVisible(Icon_Config2[i], true);
	}
}


